
package com.smallrig.mall.template.utils;

import com.baomidou.mybatisplus.core.toolkit.LambdaUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.core.toolkit.support.SerializedLambda;
import com.smallrig.mall.template.entity.Order;
import org.apache.ibatis.reflection.property.PropertyNamer;



public class LambdaHelper {


    public static <T> String columnToString(SFunction<T,?> column) {
        return columnToString(column, true);
    }

    /**
     *
     * @param column
     * @param hump 是否返回驼峰
     * @param <T>
     * @return
     */
    public static <T> String columnToString(SFunction<T,?> column, boolean hump) {
        SerializedLambda lambda = LambdaUtils.resolve(column);
        String fieldName =  PropertyNamer.methodToProperty(lambda.getImplMethodName());
        if(hump){
            return fieldName;
        }
        return toTableField(fieldName);
    }


    public static String toTableField(String filed) {
        return filed.replaceAll("[A-Z]", "_$0").toLowerCase();
    }

    public static void main(String[] args) {
        System.out.println(columnToString(Order::getUserId));
        System.out.println(columnToString(Order::getUserId,false));
    }


}
